#!/usr/bin/env python

class Population:
		
	def __init__(self,configuration,populationSize):
		self.configuration = configuration
		self.populationSize = populationSize		
		self.chromosones = list()
	
	def addChromosone(self,chromosone):
		self.chromosones.append(chromosone)

	def getChromosones(self):
		return self.chromosones		

	def determineFittestChromosone(self):
		return self.chromosones.sort()[0]	

	def applyNaturalSelectors(self):
		populationCount = self.populationSize 
		selectorCount	= len(self.configuration.naturalSelectors)

		if (selectorCount == 0):
			raise "No natural selectors present"	

		count		= populationCount/(selectorCount)
		newChromosones  = []
		
		for selector in self.configuration.naturalSelectors:
			selectedChromosones = selector.select(self,count)
			for chromosone in selectedChromosones:
				newChromosones.append(chromosone)
		
		self.chromosones = newChromosones

	
	def applyGeneticOperators(self):
		newChromosones = []	
		for operator in self.configuration.geneticOperators:
			 newChromosones = operator.operate(self)
		self.chromosones = newChromosones

		
	def getPopulationSize(self):
		return self.populationSize					

	def __str__(self):
		str = ""
		for chromosone in self.chromosones:
			str = str + chromosone.__str__() + "\n"	
		return str
